class smx_mongodb {
  require smx_os

  Exec {
    path      => [
      '/usr/bin',
      '/bin',
      '/sbin',
    ],
    logoutput => true,
    timeout   => '0',
  }

  $mongo_package_list = [
    'mongodb-org-server',
    'mongodb-org-mongos',
    'mongodb-org-shell',
  ]


  case $get_server {
    'release-hf.sumilux.com','it-deploy.foxitcloud.com': {
      yumrepo { "mongodb":
        baseurl    => "http://repo.connectedpdf.com:88/mongodb/repo/redhat/os/x86_64/RPMS/",
        mirrorlist => absent,
        descr      => "MongoDB Repository",
        enabled    => 1,
        gpgcheck   => 0, }
    }
    'ops30.sumilux.com': {
      yumrepo { "mongodb":
        baseurl    => "http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/",
        mirrorlist => absent,
        descr      => "MongoDB Repository",
        enabled    => 1,
        gpgcheck   => 0, }
    }
  }

  package { $mongo_package_list:
    ensure  => installed,
    before  => Service['mongod'],
    require => Yumrepo['mongodb'],
  }




  if $fqdn =~ /^[a-z0-9]+\.[a-z0-9-]+\.foxitcloud\.com/ {
    exec { 'remote-access' :
      command => 'sed -i "s/^bind_ip=127.0.0.1/#bind_ip=127.0.0.1/g" /etc/mongod.conf',
      onlyif  => 'grep -q "^bind_ip=127.0.0.1" /etc/mongod.conf',
      require => Exec['remote-journal-files'],
      notify  => Service['mongod'],
    }

    exec { 'remote-journal-files' :
      command => 'sed -i "s/^# nojournal=true/nojournal=true/" /etc/mongod.conf',
      onlyif  => 'grep -q "^# nojournal=true" /etc/mongod.conf',
      require => Package['mongodb-org-server'],
    }
  }

  service { 'mongod':
    ensure  => running,
    enable  => true,
    require => File['/var/lib/mongo/mongod.lock'],
  }

  file { '/vagrant/vagrant/mongoinit.sh':
    ensure => present,
    #    mode => 755, owner => root, group => root,
    source =>  "puppet:///modules/smx_mongodb/mongoinit.sh",
  }

  file { '/vagrant/vagrant/mongoinit-pro.sh':
    ensure => present,
    mode   => 755, owner => root, group => root,
    source =>  "puppet:///modules/smx_mongodb/mongoinit-pro.sh",
  }

  file { '/vagrant/vagrant/mongoinit-cpdf.sh':
    ensure => present,
    mode   => 755, owner => root, group => root,
    source =>  "puppet:///modules/smx_mongodb/mongoinit-cpdf.sh",
  }

  file { '/vagrant/vagrant/mongodata-cpdf.sh':
    ensure => present,
    mode   => 755, owner => root, group => root,
    source =>  "puppet:///modules/smx_mongodb/mongodata-cpdf.sh",
  }

  file { '/var/lib/mongo/mongod.lock':
    ensure => absent,
  }

  file { '/vagrant/vagrant/apimgt-data-init.sh':
    ensure => present,
    #    mode => 755, owner => root, group => root,
    source =>  "puppet:///modules/smx_mongodb/apimgt-data-init.sh",
  }

  file { '/vagrant/vagrant/plugininit.sh':
    ensure => present,
    #    mode => 755, owner => root, group => root,
    source =>  "puppet:///modules/smx_mongodb/plugininit.sh",
  }

}
